package com.cwl.utils;

import com.alibaba.fastjson.JSON;
import com.cwl.common.constant.UEConstants;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.jfinal.plugin.activerecord.Record;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * EasySearch
 *
 * @author cwivan
 * @version 1.0
 * @date 2019/3/8
 */
public class EasySearchOption {
	private static RedisTemplate redisTemplate = SpringContextHolder.getBean("redisTemplate");

	public static final String EASYSEARCH_COMMON = "EASYSEARCH_COMMON_";

	public static final String EASYSERCH_LABEL_KEY_TRADEWAY = "pay_method";
	public static final String EASYSERCH_LABEL_KEY_CHANNELTYPE = "pay_channel";
	public static final String EASYSERCH_LABEL_KEY_TRADESTATUS = "trade_status";
	public static final String EASYSERCH_LABEL_KEY_REFUNDSTATUS = "refund_status";
	public static final String EASYSERCH_LABEL_KEY_MCHNO = "mch_no";
	public static final String EASYSERCH_LABEL_KEY_SCENEID = "scene_id";
	public static final String EASYSERCH_LABEL_KEY_DZSCENEID = "dz_scene_id";
	public static final String EASYSERCH_LABEL_KEY_DZCHANNELTYPE = "dz_channel_type";

	private static EasySearchStore store = new EasySearchStore() {

		@Override
		public void initKv() {
			initConstant();
		}

		@Override
		public Map<String, String> getAllKvs(String labelKey) {
			ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
			String kvStr = valueOperations.get(EASYSEARCH_COMMON+labelKey);
			if(StringUtils.isBlank(kvStr)){
				return null;
			}
			return JSON.parseObject(kvStr, Map.class);
		}

		@Override
		public String getValueByKey(Map<String, String> allKvs, String labelKey, String key) {
			return allKvs.get(key);
		}

	};

	public static void initConstant(){
		ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();

		Map<String,String> tradeWay = Maps.newHashMap();
		tradeWay.put(UEConstants.TRADE_WAY_UE_QRCODE,"聚合扫码付");
		tradeWay.put(UEConstants.TRADE_WAY_UE_SCAN,"付款码刷卡付");
		tradeWay.put(UEConstants.TRADE_WAY_ALI_QRCODE,"支付宝扫码付");
		tradeWay.put(UEConstants.TRADE_WAY_ALI_SCAN,"支付宝刷卡付");
		tradeWay.put(UEConstants.TRADE_WAY_WX_QRCODE,"微信扫码付");
		tradeWay.put(UEConstants.TRADE_WAY_WX_SCAN,"微信刷卡付");
		tradeWay.put(UEConstants.TRADE_WAY_CCB_QRCODE,"建行扫码付");
		tradeWay.put(UEConstants.TRADE_WAY_CCB_SCAN,"建行刷卡付");
		tradeWay.put(UEConstants.TRADE_WAY_UNIONPAY_QRCODE,"银联扫码付");
		tradeWay.put(UEConstants.TRADE_WAY_BANK_POS,"POS上报");
		tradeWay.put(UEConstants.TRADE_WAY_ICBC_QRCODE,"工行扫码付");
		tradeWay.put(UEConstants.TRADE_WAY_UMS_QRCODE,"银联商务扫码付");
		tradeWay.put(UEConstants.TRADE_WAY_ABC_QRCODE,"农行扫码付");
		tradeWay.put(UEConstants.TRADE_WAY_YONGYI_QRCODE,"甬易扫码付");
		valueOperations.set(EASYSEARCH_COMMON+EASYSERCH_LABEL_KEY_TRADEWAY,JSON.toJSONString(tradeWay));

		Map<String,String> channelType = Maps.newHashMap();
		channelType.put(UEConstants.CHANNEL_TYPE_WX,"微信支付");
		channelType.put(UEConstants.CHANNEL_TYPE_ALI,"支付宝");
		channelType.put(UEConstants.CHANNEL_TYPE_CCB,"建行");
		channelType.put(UEConstants.CHANNEL_TYPE_CCBPOS,"建行POS");
		channelType.put(UEConstants.CHANNEL_TYPE_UNIONPAY,"银联");
		channelType.put(UEConstants.CHANNEL_TYPE_ICBC,"工行");
		channelType.put(UEConstants.CHANNEL_TYPE_UMS,"银联商务");
		channelType.put(UEConstants.CHANNEL_TYPE_ABC,"农行");
		channelType.put(UEConstants.CHANNEL_TYPE_ABCPOS,"农行POS");
		channelType.put(UEConstants.CHANNEL_TYPE_BANKPOS,"银行POS");
		channelType.put(UEConstants.CHANNEL_TYPE_YONGYI,"甬易");
		valueOperations.set(EASYSEARCH_COMMON+EASYSERCH_LABEL_KEY_CHANNELTYPE,JSON.toJSONString(channelType));

		Map<String,String> tradeStatus = Maps.newHashMap();
		tradeStatus.put(UEConstants.TRADE_STATUS_WAIT,"等待支付");
		tradeStatus.put(UEConstants.TRADE_STATUS_PAYING,"等待支付");
		tradeStatus.put(UEConstants.TRADE_STATUS_SCANED,"等待支付");
		tradeStatus.put(UEConstants.TRADE_STATUS_CLOSED,"未支付");
		tradeStatus.put(UEConstants.TRADE_STATUS_PAID,"支付成功");
		tradeStatus.put(UEConstants.TRADE_STATUS_FAIL,"未支付");
		tradeStatus.put(UEConstants.PARTIAL_REFUND,"部分退款");
		tradeStatus.put(UEConstants.FULL_REFUND,"已退款");
		valueOperations.set(EASYSEARCH_COMMON+EASYSERCH_LABEL_KEY_TRADESTATUS,JSON.toJSONString(tradeStatus));

		Map<String,String> refundStatus = Maps.newHashMap();
		refundStatus.put(UEConstants.REFUND_WAITING,"正在提交退款");
		refundStatus.put(UEConstants.REFUND_SUCCESS,"退款成功");
		refundStatus.put(UEConstants.REFUND_FAILED,"退款失败");
		refundStatus.put(UEConstants.REFUND_FINISHED,"退款成功");
		refundStatus.put(UEConstants.REFUND_CHANGE,"退款无法到账，需要人工介入");
		valueOperations.set(EASYSEARCH_COMMON+EASYSERCH_LABEL_KEY_REFUNDSTATUS,JSON.toJSONString(refundStatus));
	}

	public static void initEasySearch(){
		store.initKv();
	}

	/**
	 * 转义
	 * @param dataList
	 * @return
	 */
	public static void transfer(List<Record> dataList){
		transfer(dataList,null);
	}

	public static String transfer(String labelKey, String keyValue){
		Map<String,String> transfer = EasySearchOption.store.getAllKvs(labelKey);
		return EasySearchOption.store.getValueByKey(transfer, labelKey, keyValue);
	}

	/**
	 * 转义
	 * @param dataList
	 * @param keyNeedToTransfer 需要转义的key,传null就全部转义
	 * @return
	 */
	public static void transfer(List<Record> dataList, Set<String> keyNeedToTransfer){
		for (int i = 0; i < dataList.size(); i++) {
			Record m = dataList.get(i);
			transfer(m,keyNeedToTransfer);
		}
	}

	public static void transfer(Record data){
		transfer(data,null);
	}

	public static void transfer(Record data,  Set<String> labelKeyNeedToTransfer){
		if (labelKeyNeedToTransfer != null){
			for (String labelKey : labelKeyNeedToTransfer){
				Map<String,String> transfer = EasySearchOption.store.getAllKvs(labelKey);
				//需要转义
				if (transfer != null){
					data.set(labelKey+"_LABEL", EasySearchOption.store.getValueByKey(transfer, labelKey, data.get(labelKey)));
				}
			}
		}else{
			Set<String> keySet = Sets.newHashSet();
			keySet.addAll(Arrays.asList(data.getColumnNames()));
			for(String labelKey : keySet){
				Map<String,String> transfer = EasySearchOption.store.getAllKvs(labelKey);
				//需要转义
				if (transfer != null){
					data.set(labelKey+"_LABEL", EasySearchOption.store.getValueByKey(transfer, labelKey, data.get(labelKey)));
				}
			}
		}
	}


	public static EasySearchStore getStore() {
		return store;
	}

	public static void setStore(EasySearchStore store) {
		EasySearchOption.store = store;
	}

	public static interface EasySearchStore {
		public void initKv();

		public Map<String,String> getAllKvs(String labelKey);

		public String getValueByKey(Map<String, String> allKvs, String labelKey, String key);
	}

}
